<template>
    <el-aside :width="width">
        <el-menu background-color="#545c64" text-color="#fff" :collapse="isCollapse" :collapse-transition="false">
            <h3 v-if="!isCollapse">后台管理系统</h3>
            <h3 v-else>后台</h3>
            <!-- 遍历没有子路由的菜单 -->
            <el-menu-item v-for="item in noChildren" :key="item.path">
                <!-- 引入Element Plus的字体图标并使用 -->
                <component class="icons" :is="item.icon"></component>
                <!-- 显示菜单文字内容 -->
                <span>{{ item.label }}</span>
            </el-menu-item>

            <!-- 遍历含有子路由的菜单 -->
            <el-sub-menu v-for="item in hasChildren" :key="item.path">
                <!-- 显示父级路由和字体图标 -->
                <template #title>
                    <component class="icons" :is="item.icon"></component>
                    <span>{{ item.label }}</span>
                </template>
                <!-- 显示子级路由和其内容 -->
                <el-menu-item-group v-for="subItem in item.children" :key="subItem.path">
                    <el-menu-item>
                        <component class="icons" :is="subItem.icon"></component>
                        <span>{{ subItem.label }}</span>
                    </el-menu-item>
                </el-menu-item-group>

            </el-sub-menu>
        </el-menu>
    </el-aside>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import {isCollapseData} from '@/stores/isCollapseData'
import {storeToRefs} from 'pinia'
const router = useRouter()
const store = isCollapseData()
const {isCollapse} = storeToRefs(store)
const width = computed(() =>{
    return isCollapse.value ? '64px' : '180px'
})


const list = ref([
    {
        path: '/home',
        name: 'home',
        label: '首页',
        icon: 'house',
        url: 'Home'
    },
    {
        path: '/mall',
        name: 'mall',
        label: '商品管理',
        icon: 'video-play',
        url: 'Mall'
    },
    {
        path: '/user',
        name: 'user',
        label: '用户管理',
        icon: 'user',
        url: 'User'
    },
    {
        path: 'other',
        label: '其他',
        icon: 'location',
        children: [
            {
                path: '/page1',
                name: 'page1',
                label: '页面1',
                icon: 'setting',
                url: 'Page1'
            },
            {
                path: '/page2',
                name: 'page2',
                label: '页面2',
                icon: 'setting',
                url: 'Page2'
            }
        ]
    }
])
const noChildren = computed(() => list.value.filter(item => !item.children))
const hasChildren = computed(() => list.value.filter(item => item.children))

</script>

<style lang="less" scoped>
.icons {
    width: 18px;
    height: 18px;
    margin-right: 5px;
}

.el-menu {
    border-right: none;

    h3 {
        line-height: 48px;
        color: #fff;
        text-align: center;
    }
}

.el-aside {
    height: 100%;
    background-color: #545c64;
}
</style>